<?
require_once('class/conexion/conexion.php');


class DBAtributo
{

	public $mConexion = null;

	public function __construct() 
	{
		$this->mConexion = new Conexion($this);
	}
	
	
	public function nuevoAtributo($atributo, $tipo)
	{
		$sql ="INSERT INTO `atributo` (`NOMBRE`,`TIPO`)
		VALUES ('".mysql_real_escape_string($atributo)."','".mysql_real_escape_string($tipo)."') ";
		$this->mConexion->sql($sql);

		return $tipo;
	}
	
	public function editarAtributo($idAtributo, $nombre, $tipo)
	{
	    $sql[]= "UPDATE `atributo` set NOMBRE= '$nombre', TIPO = '$tipo' WHERE ID = $idAtributo";
	    $this->mConexion->sql($sql);
	}
	
	public function borrarAtributo($id)
	{
	    $sql =" DELETE FROM `atributo` WHERE ID=".mysql_real_escape_string($id)." ";
	    $this->mConexion->sql($sql);
	}
	
	public function mostrarAtributo($id)
	{
	    $sql[]= "SELECT * FROM `atributo` where ID=".mysql_real_escape_string($id)."";
	    return mysql_fetch_array($this->mConexion->sql($sql));
	}

	public function listarAtributos()
	{
		$sql = "SELECT * FROM ATRIBUTO";
		return $this->mConexion->sql($sql);
	}
	
	public function listarAtributosRedNodo($idRed)
	{  
		$sql = "SELECT * FROM ATRIBUTOS_RED, ATRIBUTO WHERE ID_ATRIBUTO=ID AND ATRIBUTOS_RED.TIPO='NODO'  AND ID_RED=".mysql_real_escape_string($idRed)."  ";
		return $this->mConexion->sql($sql);
	}
	
	
	public function asociarAtributos($atributo, $red, $tipo)
	{		
		$sql ="INSERT INTO `atributos_red` (`ID_RED`, `ID_ATRIBUTO`, `TIPO`)
				VALUES (".mysql_real_escape_string($red).
				", ".mysql_real_escape_string($atributo).
				", '".mysql_real_escape_string($tipo)."')";	
		$this->mConexion->sql($sql);

		return true;
	}
	
	public function listarAtributosRedEnlace($idRed)
	{  
		$sql = "SELECT * FROM ATRIBUTOS_RED, ATRIBUTO WHERE ID_ATRIBUTO=ID AND ATRIBUTOS_RED.TIPO='ENLACE'  AND ID_RED=".mysql_real_escape_string($idRed)."  ";
		return $this->mConexion->sql($sql);
	}
	
	public function atributoEnUso($id)
	{
		$sql[]="select * FROM `atributos_red` WHERE ID_ATRIBUTO=".mysql_real_escape_string($id)." ";
		$registro=mysql_fetch_row($this->mConexion->sql($sql));
		if (!empty($registro)) return true;
		else return false;
	}
	
	public function hayAtributos()
	{
		$sql[]="select * FROM `atributo`";
		$registro=mysql_fetch_row($this->mConexion->sql($sql));
		if (!empty($registro)) return true;
		else return false;
		
	}
	
	
	public function tiposDeAtributos()
	{
		$sql[]="select * FROM `TIPO`";
		return ($this->mConexion->sql($sql));
	}
	
	
	public function contarAtributosXNombre($nombre)
	{
		$sql = "SELECT COUNT(*) AS TOTAL FROM ATRIBUTO WHERE NOMBRE = '".$nombre."'";
		return mysql_fetch_array($this->mConexion->sql($sql));
	}
	
	
	
	
}
?>